package com.peng.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.edu.entity.Comment;
import com.peng.edu.entity.Teacher;
import com.peng.edu.service.CommentService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/edu/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;

    //查询评论列表
    @GetMapping("/getCommentInfoByCourseId/{page}/{limit}/{courseId}")
    public Result getCommentInfoByCourseId(@PathVariable Integer page,
                                           @PathVariable Integer limit,
                                           @PathVariable String courseId){
        Page<Comment> commentPage = new Page<>(page, limit);
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getCourseId,courseId);
        Page<Comment> commentIPage = (Page<Comment>) commentService.page(commentPage, queryWrapper);
        List<Comment> records = commentIPage.getRecords();
        long current = commentIPage.getCurrent();
        long pages = commentIPage.getPages();
        long size = commentIPage.getSize();
        long total = commentIPage.getTotal();
        //是否有下一页
        boolean hasNext = commentIPage.hasNext();
        //是否有上一页
        boolean hasPrevious = commentIPage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return Result.ok().data(map);
    }

    //添加评论
    @PostMapping("/addComment")
    public Result addComment(@RequestBody Comment comment, HttpServletRequest request){
        commentService.addComment(comment,request);
        return Result.ok();
    }
}

